//
//  LMRTabsScrollView.m
//  EditTabDemo
//
//  Created by 李浩源 on 2021/2/28.
//  Copyright © 2021 lhy. All rights reserved.
//

#import "LMRTabsScrollView.h"
#import "LHYDefine.h"

@interface LMRTabsScrollView ()

@property (nonatomic, strong) NSMutableArray *tabBtnArr;

@end

@implementation LMRTabsScrollView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

- (void)setUI {
    self.backgroundColor = UIColor.whiteColor;
    self.showsVerticalScrollIndicator = NO;
    self.showsHorizontalScrollIndicator = NO;
    
    [self initAddFileTab];
}

- (void)initAddFileTab {
    _tabAddFileView = [[LMRTabsView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    _tabAddFileView.tag = 100;
    _tabAddFileView.btnTitle = @" +添加文件";
    [_tabAddFileView.deleteBtn removeFromSuperview];
    WeakSelf;
    _tabAddFileView.tabSelectedBlock = ^(NSInteger tagValue) {
        StrongSelf;
        [strongSelf tabAdd];
        
    };
    [self addSubview:_tabAddFileView];
}

/// 删除所有
- (void)tabDeleteAll {
    [self.tabBtnArr removeAllObjects];
    
    for (LMRTabsView *tabsView in self.subviews) {
//        if (tabsView.tag != 100) {
            [tabsView removeFromSuperview];
//        }
    }
    //2、根据新数组 重新添加元素
    [self resetTabBtn];
}

#pragma mark - 根据数据初始化
/// 根据数据初始化
- (void)initTabsScrollViewWithData:(NSMutableArray *)dataArr {
    for (int i = 0; i < dataArr.count; i++) {
        _tabsView = [[LMRTabsView alloc] initWithFrame:CGRectMake(i * 100, 0, 100, 44)];
        _tabsView.tag = i + 10;
        _tabsView.btnTitle = [NSString stringWithFormat:@"文件%ld", i];
        WeakSelf;
        _tabsView.tabSelectedBlock = ^(NSInteger tagValue) {
            StrongSelf;
            [strongSelf tabSelectedWithTag:tagValue];
        };
        _tabsView.tabDeleteBlock = ^(NSInteger tagValue) {
            StrongSelf;
            [strongSelf tabDeleteWithTag:tagValue];
        };
        
        [self addSubview:_tabsView];
        [self.tabBtnArr addObject:_tabsView];
    }
    [self initAddFileTab];
    _tabAddFileView.frame = CGRectMake(_tabsView.frame.size.width + _tabsView.frame.origin.x, 0, 100, 44);
    self.contentSize = CGSizeMake(100 + 100 * self.tabBtnArr.count, 44);
    
    
//    NSInteger index = [self.tabBtnArr indexOfObject:_tabsView];
    
    if (self.tabsAddBlock) {
        self.tabsAddBlock(0, self.tabBtnArr.count);
    }
    
    //更新选中按钮
    [self tabSelectedWithIndex:0];
}

- (void)updateTabsScrollViewWithCount:(NSInteger)count {
//    NSInteger count = self.tabBtnArr.count;
    NSLog(@"Arr count:  %ld", count);
    
    _tabsView = [[LMRTabsView alloc] initWithFrame:CGRectMake(count * 100, 0, 100, 44)];
    
    LMRTabsView *view = self.tabBtnArr.lastObject;
    _tabsView.tag = count ? view.tag + 1 : 10;
    
    _tabsView.btnTitle = [NSString stringWithFormat:@"文件%ld", _tabsView.tag - 10];
    
    WeakSelf;
    _tabsView.tabSelectedBlock = ^(NSInteger tagValue) {
        StrongSelf;
        [strongSelf tabSelectedWithTag:tagValue];
    };
    _tabsView.tabDeleteBlock = ^(NSInteger tagValue) {
        StrongSelf;
        [strongSelf tabDeleteWithTag:tagValue];
    };
    
    [self addSubview:_tabsView];
    [self.tabBtnArr addObject:_tabsView];
    

    
    _tabAddFileView.frame = CGRectMake(_tabsView.frame.size.width + _tabsView.frame.origin.x, 0, 100, 44);
    
    self.contentSize = CGSizeMake(100 + 100 * self.tabBtnArr.count, 44);
    
    
    NSInteger index = [self.tabBtnArr indexOfObject:_tabsView];
    
    if (self.tabsAddBlock) {
        self.tabsAddBlock(index, self.tabBtnArr.count);
    }
    
    //更新选中按钮
    [self tabSelectedWithTag:_tabsView.tag];
}

/// 删除后重新布局UI
- (void)resetTabBtn {
    
    for (int i = 0; i < self.tabBtnArr.count; i++) {
        LMRTabsView *tabsView = self.tabBtnArr[i];
        tabsView.frame = CGRectMake(i * 100, 0, 100, 44);
        tabsView.btnTitle = [NSString stringWithFormat:@"文件%ld", tabsView.tag - 10];
        WeakSelf;
        tabsView.tabSelectedBlock = ^(NSInteger tagValue) {
            StrongSelf;
            [strongSelf tabSelectedWithTag:tagValue];
        };
        tabsView.tabDeleteBlock = ^(NSInteger tagValue) {
            StrongSelf;
            [strongSelf tabDeleteWithTag:tagValue];
        };
        
        [self addSubview:tabsView];
        
        tabsView.backgroundColor = UIColor.whiteColor;
        if (i == 0) {
            tabsView.backgroundColor = UIColor.lightGrayColor;
        }
        
        if (i == self.tabBtnArr.count - 1) {
            _tabAddFileView.frame = CGRectMake(tabsView.frame.size.width + tabsView.frame.origin.x, 0, 100, 44);
        }
    }
    
    if (self.tabBtnArr.count == 0) {
        _tabAddFileView.frame = CGRectMake(0, 0, 100, 44);
    }
    
    self.contentSize = CGSizeMake(100 + 100 * self.tabBtnArr.count, 44);
}


#pragma mark - 增加文件
///点击”+添加文件“
- (void)tabAdd {
//    [self addPDFDocumentFromFiles];
    if (self.tabsOpenBlock) {
        self.tabsOpenBlock();
    }
}

#pragma mark - 删除文件
/// 点击tab删除
- (void)tabDeleteWithTag:(NSInteger)tagValue {
    NSLog(@"tagValue:  %ld",tagValue);
    LMRTabsView *tabsView = [self viewWithTag:tagValue];
    //删除元素的 数组下标
    NSInteger indexDelete = [self.tabBtnArr indexOfObject:tabsView];
    
    if (self.tabBtnArr.count > 0) {
        [self.tabBtnArr removeObject:tabsView];
    }
    //1、先移除所有，
    for (LMRTabsView *view in self.subviews) {
        if (view.tag != 100) {
            [view removeFromSuperview];
        }
    }
    //2、根据新数组 重新添加元素
    [self resetTabBtn];
    
    if (self.tabsDeleteBlock) {
        self.tabsDeleteBlock(indexDelete, self.tabBtnArr.count);
    }
}

#pragma mark - 查看文件
/// 点击tab  tag
- (void)tabSelectedWithTag:(NSInteger)tagValue {
    
    for (LMRTabsView *view in self.tabBtnArr) {
        view.backgroundColor = UIColor.whiteColor;
        view.deleteBtn.hidden = YES;
    }
    LMRTabsView *tabView = [self viewWithTag:tagValue];
    tabView.deleteBtn.hidden = NO;
    tabView.backgroundColor = UIColor.lightGrayColor;
    
    NSInteger index = [self.tabBtnArr indexOfObject:tabView];
    
    if (self.tabsSelectedBlock) {
        self.tabsSelectedBlock(index, self.tabBtnArr.count);
    }
    
    // scrollView 最多容纳完整按钮个数
    NSInteger maxCount = self.frame.size.width / 100;
    // 滚动到 当前按钮的右边的按钮
    if (self.tabBtnArr.count > maxCount) {
        CGRect rect = CGRectMake(tabView.frame.origin.x + 100, 0, 100, 44);
        [self scrollRectToVisible:rect animated:YES];
    }
}
/// 点击tab  index
- (void)tabSelectedWithIndex:(NSInteger)index {
    
    for (LMRTabsView *view in self.tabBtnArr) {
        view.backgroundColor = UIColor.whiteColor;
        view.deleteBtn.hidden = YES;
    }
    if (self.tabBtnArr.count) {
        LMRTabsView *tabView = self.tabBtnArr[index];
        tabView.deleteBtn.hidden = NO;
        tabView.backgroundColor = UIColor.lightGrayColor;
    }
    
    if (self.tabsSelectedBlock) {
        self.tabsSelectedBlock(index, self.tabBtnArr.count);
    }
}

#pragma mark - 懒加载
- (NSMutableArray *)tabBtnArr {
    if (!_tabBtnArr) {
        _tabBtnArr = [[NSMutableArray alloc] init];
    }
    return _tabBtnArr;
}

@end
